import {  Mesh, MeshBasicMaterial, BoxGeometry } from 'three';
import { Params } from './baseModel.js';
import { WORDS } from './WORDS.js';
import { Helper } from '../helper.js';

interface PlaneParams extends Params {
    width: number,
    depth: number,
    textureUrl: string,
    color: string,
}

class Plane {
    id: string
    obj: Mesh<BoxGeometry, MeshBasicMaterial>
    type: string
    platformIndex?: number
    params: PlaneParams
    constructor(obj: Mesh<BoxGeometry, MeshBasicMaterial>, params: PlaneParams) {
        if (!obj.isMesh) {
            throw new Error(WORDS.ERROR.NOT_MESH);
        }

        if (params.id) {
            this.id = params.id
        } else {
            this.id = Date.now().toString()
            params.id = this.id
        }

        this.params = params
        this.obj = obj
        this.obj.name = this.id
        this.type = WORDS.OBJNAME.MODEL_PLANE
        this.obj.userData.type = this.type
    }

    updateParams() {
        const mtl = this.obj.material
        this.params.color = Helper.getHexColorStr(mtl.color)
        // mtl.map?.image
        this.params.width = this.obj.geometry.parameters.width
        this.params.depth = this.obj.geometry.parameters.depth
        this.params.opacity = mtl.opacity
    }

    toJSON() {
        this.updateParams()
        return this.params
    }
}

export { Plane };
export type { PlaneParams };

